package cn.kgc.mcartoon.service.impl;

import cn.kgc.mcartoon.condition.CartoonCondition;
import cn.kgc.mcartoon.constans.Constants;
import cn.kgc.mcartoon.dto.CartoonDto;
import cn.kgc.mcartoon.dto.HotDto;
import cn.kgc.mcartoon.dto.ResultDto;
import cn.kgc.mcartoon.entity.MmCartoonClassificationDicDO;
import cn.kgc.mcartoon.entity.MmCartoonDO;
import cn.kgc.mcartoon.mapper.MmCartoonClassificationDicMapper;
import cn.kgc.mcartoon.mapper.MmCartoonMapper;
import cn.kgc.mcartoon.service.MmCartoonService;

import cn.kgc.mcartoon.utils.JsonRedisClient;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 漫画表 服务实现类
 * </p>
 *
 * @author christopher
 * @since 2021-09-23
 */
@Service
public class MmCartoonServiceImpl extends ServiceImpl<MmCartoonMapper, MmCartoonDO> implements MmCartoonService {

    @Autowired
    private JsonRedisClient jsonRedisClient;

    @Autowired
    private MmCartoonMapper cartoonMapper;

    @Autowired
    private MmCartoonClassificationDicMapper cartoonClassificationDicMapper;

    @Override
    public List<CartoonDto> selectOriginalList(CartoonCondition condition) {

        List<CartoonDto> cartoons = cartoonMapper.selectCartoonList(condition);

        this.cartoonFormat(cartoons);

        return cartoons;
    }

    @Override
    public List<HotDto> selectHotList(CartoonCondition condition) {
        //创建热门排行list
        List<HotDto> hotDtos = new ArrayList<>();

        //全局排行
        List<CartoonDto> allCartoons = null;
        //国内排行
        List<CartoonDto> cnCartoons = null;
        //日本排行
        List<CartoonDto> jpCartoons = null;


        Object allCartoonsObj = jsonRedisClient.get(Constants.Redis.KEY_HOME_RANKING);
        if(null != allCartoonsObj){
            String allCartoonsJson = JSON.toJSONString(allCartoonsObj);
            allCartoons = JSON.parseObject(allCartoonsJson,List.class);
        }else {
            allCartoons = cartoonMapper.selectCartoonList(condition);
            jsonRedisClient.set(Constants.Redis.KEY_HOME_RANKING,allCartoons);
        }

        //调用封装方法封装到集合中
        this.areaFormat(Constants.Home.OVERALL,allCartoons,hotDtos);


        Object cnCartoonsObj = jsonRedisClient.get(Constants.Redis.KEY_HOME_RANKING + Constants.Home.CHINA);
        if (null != cnCartoonsObj){
            String cnCartoonsJson = JSON.toJSONString(cnCartoonsObj);
            cnCartoons = JSON.parseObject(cnCartoonsJson,List.class);
        }else {
            //设置查询条件为中国
            condition.setAreaId(Constants.Home.CHINA);
            cnCartoons = cartoonMapper.selectCartoonList(condition);
            jsonRedisClient.set(Constants.Redis.KEY_HOME_RANKING + Constants.Home.CHINA,cnCartoons);
        }

        this.areaFormat(Constants.Home.CHINA,cnCartoons,hotDtos);


        Object jpCartoonsObj = jsonRedisClient.get(Constants.Redis.KEY_HOME_RANKING + Constants.Home.JAPAN);
        if (null != jpCartoonsObj){
            String jpCartoonsJson = JSON.toJSONString(jpCartoonsObj);
            jpCartoons = JSON.parseObject(jpCartoonsJson,List.class);
        }else {
            //设置查询条件为日本
            condition.setAreaId(Constants.Home.JAPAN);
            jpCartoons = cartoonMapper.selectCartoonList(condition);
            jsonRedisClient.set(Constants.Redis.KEY_HOME_RANKING + Constants.Home.JAPAN,jpCartoons);
        }

        this.areaFormat(Constants.Home.JAPAN,jpCartoons,hotDtos);

        return hotDtos;
    }

    @Override
    public ResultDto seletactives() {

        QueryWrapper<MmCartoonDO> mmCartoonDOQueryWrapper = new QueryWrapper<>();

        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(sf.format(date));
        String day = sf.format(date);

//        cartoonMapper.selectCartoonList(mmCartoonDOQueryWrapper.le("lastCapterUpTime",day));

        return null;
    }

    private void areaFormat(Integer areaId,List<CartoonDto> cartoons,List<HotDto> hotDtos) {
        if(!CollectionUtils.isEmpty(cartoons)){
            hotDtos.add(new HotDto(areaId,cartoons));
        }

    }

    private void cartoonFormat(List<CartoonDto> cartoons) {

        if(!CollectionUtils.isEmpty(cartoons)){
            cartoons.forEach(cartoon -> {
                this.cartoonDtoFormat(cartoon,cartoons);
            });
        }
        //return cartoons;
    }

    /**
     * 根据CartoonId查询主题
     * @param cartoon
     * @param cartoonDtos
     */
    private void cartoonDtoFormat(CartoonDto cartoon,List<CartoonDto> cartoonDtos) {

        List<String> themes = null;
        //先查看缓存中是否有我们要的内容
        Object o = jsonRedisClient.get(Constants.Redis.KEY_HOME_ORIGINAL+cartoon.getCartoonId());
        if (null != o){
            String themeJson = JSON.toJSONString(o);
            themes = JSON.parseObject(themeJson,List.class);
            cartoon.setThemes(themes);
        }else {
            themes = cartoonClassificationDicMapper.selectThemesByCartoonId(cartoon.getCartoonId());
            jsonRedisClient.set(Constants.Redis.KEY_HOME_ORIGINAL+cartoon.getCartoonId(),themes);
            cartoon.setThemes(themes);
        }

    }


}
